package com.cuit.controller;

import com.cuit.pojo.Patient;
import com.cuit.service.Impl.PatientServiceImpl;
import com.cuit.service.PatientService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


public class PatientRegisterServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PatientService patientService = new PatientServiceImpl();
        String p_id = req.getParameter("p_id");
        String p_name = req.getParameter("p_name");
        String p_password = req.getParameter("p_password");
        String p_sex = req.getParameter("p_sex");
        String p_age = req.getParameter("p_age");
        String id_card = req.getParameter("id_card");
        Patient patient = new Patient(p_id, p_name, p_password, p_sex, p_age,id_card);
        req.getSession().setAttribute("patient",patient);

        if (patientService.existPatient(patient.getP_id())) {
            System.out.println("手机号已存在，请重新输入注册！！！");
            PrintWriter writer=resp.getWriter();
            writer.write("<h3>手机号已被注册，请<a href='patient/login.jsp'>登录</a>！！</h3>");
//            req.getRequestDispatcher("patient/register.jsp").include(req,resp);
        } else {
            System.out.println("注册成功！");
            patientService.register(patient);
            resp.sendRedirect("patient/login.jsp");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
